/**
 * 
 */
package com.module.bp.ui;

import com.dalonedrau.jogl.engine.GlobalConsts;
import com.dalonedrau.jogl.engine.Interactive;
import com.dalonedrau.jogl.engine.ProjectConstants;
import com.dalonedrau.jogl.opengl.sprite.BasicQuadSpriteObject;
import com.dalonedrau.jogl.opengl.sprite.SpriteBase;
import com.dalonedrau.jogl.opengl.ui.Label;
import com.dalonedrau.jogl.opengl.ui.panel.LandscapePagePanel;
import com.dalonedrau.jogl.opengl.ui.panel.Panel;
import com.dalonedrau.pool.PooledStringBuilder;
import com.dalonedrau.pool.StringBuilderPool;
import com.module.bp.player.BPCharacter;
import com.module.bp.player.BPConsts;
import com.module.bp.player.BPPlayer;

/**
 * @author Donald
 *
 */
public final class PlayerViewer extends LandscapePagePanel {
	/** the id of the PC displayed in the viewer. */
	private int playerId = -1;
	/** the label displaying the PC's face. */
	private Label lblFace;
	/** the label displaying the PC's face. */
	private Label lblStats;
	/**
	 * Creates a new instance of {@link PlayerViewer}.
	 */
	PlayerViewer() {
		super(Panel.LANDSCAPE_CENTER);
		SpriteBase sb = new BasicQuadSpriteObject();
		sb.setRed(0);
		sb.setGreen(0);
		sb.setBlue(0);
		sb.setAlpha(0);
		sb.setWidth(48);
		sb.setHeight(48);
		lblFace = new Label(sb);
		sb = null;
		lblFace.setBorder(new SimplePlayerBorder());
		super.addChild(lblFace);
		lblStats = 
			new Label(ProjectConstants.getInstance().getFontH2(), 
					"\t \t \n \n \n");
		lblStats.setBorder(new SimplePlayerBorder());
		super.addChild(lblStats);
		super.setVisible(false);
	}
	/**
	 * Gets the id of the PC displayed in the viewer.
	 * @return <code>int</code>
	 */
	public int getPlayerId() {
		return playerId;
	}
	/**
	 * Sets the id of the PC displayed in the viewer.
	 * @param val the id to set
	 * @throws Exception if an error occurs
	 */
	public void setPlayerId(final int val) throws Exception {
		playerId = val;
		if (val != -1) {
			BPCharacter pc = 
				(BPCharacter) Interactive.getInstance().getIO(playerId);
			BPPlayer.getInstance().computePlayerFullStats(pc);
			lblFace.setSprite(pc.onSmallPortrait());
			PooledStringBuilder sb = 
				StringBuilderPool.getInstance().getStringBuilder();
			sb.append(pc.getName());
			int len = 10 - pc.getName().length;
			for (int i = 0; i < len; i++) {
				sb.append(" ");
			}
			sb.append("\nAT: ");
			sb.append((int) pc.getFullAbilityScore(BPConsts.ABILITY_COMBAT));
			sb.append("\nHP: ");
			sb.append((int) pc.getFullAbilityScore(BPConsts.ABILITY_ENDURANCE));
			sb.append("/");
			sb.append((int) pc.getFullAbilityScore(BPConsts.ABILITY_MAX_ENDURANCE));
			lblStats.setText(sb.toString());
			sb.returnToPool();
			pc = null;
			super.setVisible(true);
		} else {
			super.setVisible(false);
		}
	}
}
